<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/8/14 0014
 * Time: 下午 13:38
 */

namespace app\announce\controller;


use think\Controller;

class CrontabController extends Controller
{
//	public function _initialize()
//	{
//		if (!defined("CRONTAB_TOKEN")) {
//			echo "非法请求";
//			exit;
//		}
//	}

	public function index()
	{
		//crontab格式：*/15 * * * * php cli Crontab
		$stime = microtime(TRUE);//获取程序开始执行的时间
		/*
		 * 分批取数据
		 * 每次取$num个
		 * 循环完再取下一批，防止爆内存
		 */
		$limit = 0;
		$num = 5000;
		while (TRUE) {
			$info = db("peers")->limit($limit, $num)->select();
			if (!$info) {
				break;
			}

			try {
				foreach ($info as $k => $v) {
					$diff = round((time() - strtotime($v['lasttime'])) / 60, 2);//时间差，单位分钟
					$where = [
						"torrentid" => ["=", $v['torrentid']],
						"type" => ["=", $v['type']],
						"userid" => ["=", $v['userid']],
						"lasttime" => ["=", $v['lasttime']]
					];
					if ($diff > 30) {
						db("peers")->where($where)->delete();
					}
				}
			} catch (\Exception $exception) {
				$this->feedback("清理冗余");
			}

			$limit += $num;
		}

		$limit = 0;
		$num = 5000;
		while (TRUE) {
			$info = db("snatched")->limit($limit, $num)->select();
			if (!$info) {
				break;
			}
			try {
				$uid = 1;
				foreach ($info as $k => $v) {
					$uploaded = db("snatched")->where("userid", $v['userid'])->sum("uploaded");
					$downloaded = db("snatched")->where("userid", $v['userid'])->sum("downloaded");
					$seedtime = db("snatched")->where("userid", $v['userid'])->sum("seedtime");
					$leechtime = db("snatched")->where("userid", $v['userid'])->sum("leechtime");
					$update = [
						"uploaded" => $uploaded,
						"downloaded" => $downloaded,
						"seedtime" => $seedtime,
						"leechtime" => $leechtime
					];
					if ($uid !== $v['userid']) {
						db("users")->where("uid", $v['userid'])->update($update);
					}
					$uid = $v['userid'];

					$seeders_where = [
						"torrentid" => ["=", $v['torrentid']],
						"type" => ["=", $v['type']],
						"seeder" => ["=", "1"]
					];
					$seeders = db("peers")->where($seeders_where)->count("torrentid");
					$leecher_where = [
						"torrentid" => ["=", $v['torrentid']],
						"type" => ["=", $v['type']],
						"seeder" => ["=", "0"]
					];
					$leechers = db("peers")->where($leecher_where)->count("torrentid");
					db(getTorrentDB($v['type']))->where("id", $v['torrentid'])->update(["seeder" => $seeders, "leecher" => $leechers]);
				}
			} catch (\Exception $exception) {
				$this->feedback("更新用户数据");
			}

			$limit += $num;
		}

		$limit = 0;
		$num = 5000;
		while (TRUE) {
			$info = db("user_login_fail")->limit($limit, $num)->select();
			if (!$info) {
				break;
			}
			try {
				foreach ($info as $k => $v) {
					if (strtotime($v['locktime']) != "-62170012800") {
						$diff = round((time() - strtotime($v['locktime'])) / 3600, 2);//时间差，单位小时
						if ($diff > 6) {
							db("user_login_fail")->where("id", $v['id'])->update([
								"num" => "",
								"locktime" => ""
							]);
						}
					}
				}
			} catch (\Exception $exception) {
				$this->feedback("解除IP锁定");
			}

			$limit += $num;
		}

		$etime = microtime(TRUE);//获取程序执行结束的时间
		$total = $etime - $stime;//计算差值
		echo "执行共使用了" . round($total, 2) . "秒";
	}

	public function feedback($content)
	{
		$msg = $content . "失败";
		echo $msg . "\n";
		logSave($msg, $this->request->controller(), 2);
		exit;
	}
}